# 最终修复video_processor.py文件中的语法错误
import os

# 文件路径
file_path = r'd:\未知\yolo_apply\video_processing\video_processor.py'

# 读取文件内容
with open(file_path, 'r', encoding='utf-8') as f:
    content = f.read()

# 查找错误区域（使用更灵活的方式）
start_marker = "# 错误率过高时触发警告和恢复"
end_marker = "error_count = 0"

if start_marker in content and end_marker in content:
    # 找到开始和结束位置
    start_pos = content.find(start_marker)
    end_pos = content.find(end_marker, start_pos) + len(end_marker)
    
    # 创建新的代码块
    new_code_block = '''                # 错误率过高时触发警告和恢复
                import time
                current_time = time.time()
                if error_count > 10 and (current_time - last_error_time > 30):
                    logger.warning("处理错误率过高: " + str(self.camera_id) + ", 最近错误数: " + str(error_count))
                    if hasattr(self, '_handle_error'):
                        self._handle_error(Exception('30秒内出现' + str(error_count) + '个错误'), 'high_error_rate')
                    last_error_time = current_time
                    error_count = 0'''
    
    # 替换代码块
    new_content = content[:start_pos] + new_code_block + content[end_pos:]
    
    # 写入修复后的内容
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(new_content)
    
    print(f"已成功重写错误区域并修复 {file_path} 文件")
else:
    print("未找到指定的代码区域")